package org.example;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CjSeerStar {
        public static void main(String[] args) throws Exception {
            File file = new File("D:\\浏览器下载");
            String name = "重聚脱机日常【1.8.7】.exe";
            //账户
            String account = "D:\\浏览器下载\\账户.txt";
            Begin(file, name, account);
            System.out.println("ceshi");
            System.out.println("ceshi2");

        }
        
        //开始重聚
        public static void Begin(File file, String name, String account) throws Exception {
            Map<String,String> map = searchAccount(account);
            for (Map.Entry<String,String> entry:map.entrySet()){
                String names=entry.getKey();
                String password= entry.getValue();
                try {
                    searchfile(file, name);

                    Thread.sleep(1000);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                Thread.sleep(3000);
                login(names, password);
            }
        }
        //登录
        public static void login(String name, String password) throws Exception {
            Robot robot = new Robot();

            // 3. 移动鼠标到“账号输入框”并点击（需提前测试坐标）
            robot.mouseMove(500, 220); // x=300, y=200（根据实际调整）
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

            // 4. 输入账号
            type(robot, name);


            // 5. 按 Tab 或点击密码框（或直接移动）
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(300);
// 全选：Ctrl + A
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            Thread.sleep(100);
// 删除：Delete
            robot.keyPress(KeyEvent.VK_DELETE);
            robot.keyRelease(KeyEvent.VK_DELETE);
            Thread.sleep(100);
            // 6. 输入密码
            type(robot, password);

            // 7. 按 Tab 到登录按钮 或 直接点击登录按钮人呢额密码
            robot.mouseMove(500, 310); // 登录按钮坐标
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            Thread.sleep(3000);
            System.out.println("登录操作已发送。");

            //日常
//            robot.mouseMove(780, 620);
//            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
//            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
//            Thread.sleep(1000);


            // 7. 按 Tab 到登录按钮 或 直接点击登录按钮
            robot.mouseMove(1080, 180); // 布雷按钮坐标
            robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            System.out.println("布雷关卡已发送。");


        }




        // 模拟键盘输入（逐字符，避免输入法问题）
        private static void type(Robot robot, String text) {
            for (char c : text.toCharArray()) {
                int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
                if (KeyEvent.CHAR_UNDEFINED == keyCode) {
                    continue;
                }
                boolean isShift = Character.isUpperCase(c);
                if (isShift) {
                    robot.keyPress(KeyEvent.VK_SHIFT);
                }
                robot.keyPress(keyCode);
                robot.keyRelease(keyCode);
                if (isShift) {
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                }
                try { Thread.sleep(50); } catch (InterruptedException e) {}
            }
        }

        //递归查找
        public static void searchfile(File file, String name) throws Exception {
            if (file==null||!file.exists()||file.isFile()){
                return;
            }else if (file.isDirectory()){

                File[] files = file.listFiles();
                if (files!=null&&files.length>0){
                    for (File f:files){
                        if (f.isFile()){
                            if (f.getName().contains(name)){
                                System.out.println("找到"+name+"在"+f.getAbsolutePath());
                                Runtime runtime = Runtime.getRuntime();
                                runtime.exec(f.getAbsolutePath());
                            }
                        }
                        else {
                            searchfile(f,name);
                        }
                    }
                }
            }
        }

        //查找账户文件并且读取文件内容
        public static Map<String,String> searchAccount(String acount) throws IOException {
            Map<String,String>  map = new HashMap<>();
            try(BufferedReader br = new BufferedReader(new FileReader(acount));) {

                String line;
                int i = 0;

                //map存储账号和密码
                while ((line = br.readLine()) != null) {

                    String[]  arr = line.split("，|,");

                    map.put(arr[i],arr[i+1]);

                }

                return map;

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }
    }




